/*************** 刘灿  2018年04月23日 16时21分   系统管理 -- 角色设置    ********************************/

//# sourceURL=role.js

var basePath = "admin/role/"; 

var ModulesList = null;  // 全局变量父集菜单

var moduleHtml = "";

$(function(){
	// 绑定查询事件
	$('#queryRoleBtn').click(function(){
		loadRoleList();
	});
	loadRoleList();
	loadModuleList();
})

/*
 * 获取角色list
 */
function loadRoleList(){
	$.ajax({
		type: 'POST',
		url: basePath + 'loadRoleList',
		dataType: 'json',
		data:{'roleName':$('#roleName').val()},
		success: function(result){
			if(result.success == true){
				var roleList = result.data;
				$('#dataSum').text(roleList.length);
				var trs = "";
				for(var i=0;i<roleList.length;i++){
					var role = roleList[i];
					trs += '<tr class="text-c">'
								+ '<td><input type="checkbox" value="'+role['id']+'"></td>'
								+ '<td>'+role['id']+'</td>'
								+ '<td>'+role['roleName']+'</td>'
								+ '<td>'+role['roleDesc']+'</td>'
								+ '<td class="td-status">'+(role.active == 1 ? '<span class="label label-success radius">已启用</span>' : '<span class="label label-warning radius">已停用</span>')+'</td>' // 
								+ '<td class="td-manage">' +
								(role.active == 1 ? '<a style="text-decoration:none" onClick="role_active(this)" href="javascript:;" title="停用"><i class="Hui-iconfont">&#xe631;</i></a>'
										           : '<a style="text-decoration:none" onClick="role_active(this)" href="javascript:;" title="启用"><i class="Hui-iconfont">&#xe615;</i></a>')
										+'<a title="编辑" href="javascript:;" onclick="role_edit(this,'+role['id']+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>'
										+'<a title="编辑权限菜单" href="javascript:;" onclick="role_permission('+role['id']+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>'
										+'<a title="删除" href="javascript:;" onclick="role_del(this,'+role['id']+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>'
								+'</td>'
							+ '</tr>';
				}
				$('tbody').html(trs);
			}
		},
		error:function(result) {
			console.log(result.message);
		},
	});	
}


/*
 * 获取菜单list
 */
function loadModuleList(){
	$.ajax({
		type: 'POST',
		url: '/admin/module/loadModuleList',
		dataType: 'json',
		success: function(result){
			ModulesList = result.data;
			// 初始化界面 start
			var parents = ModulesList.filter(function(item,index,array){
				return item["parentOperationId"] == '-1';
			});
			moduleHtml = "";
			for(var i=0;i<parents.length;i++){
				var parentId = parents[i]["operationId"];
				var childs = ModulesList.filter(function(item,index,array){
					return item["parentOperationId"] == parentId ;
				});
				if(childs.length > 0){
					var dl = '<dl class="permission-list"><dt><label>'+parents[i]['operationName']+'</label></dt><dd id="">';
					for(var j=0;j<childs.length;j++){
						dl += '<label class="ml-20"><input type="checkbox" value="'+childs[j]['operationId']+'" name="">'+childs[j]['operationName']+'</label>';
					}
					dl+="</dd>";
					moduleHtml += dl;
				}
			}
		}
	});
}

/* 新增弹窗 */
function role_add(){
	layer.open({
		type: 2,
		title: '新增角色',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'toPage?url=pages/admin/role/role-info',
		area: ['350px', '250px'],
		end:function(){
			if($('#childFormMsg').val() == "保存成功"){
				loadRoleList();
			}
		}
	});
}

/* 编辑弹窗 */
function role_edit(obj,id){
	layer.open({
		type: 2,
		title: '编辑角色',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'toPage?url=pages/admin/role/role-info',
		area: ['350px', '250px'], 
		success: function (layero, index) { // 层弹出后的成功回调方法  ----  layero 当前层dom、index 当前层索引
			// 获取弹出层上对应的控件对象
			var dom = layero.find("iframe")[0].contentWindow.document;
			$("#id", dom).val($(obj).parents('tr').children('td').eq(1).text()); // 主键 
			$("#roleName", dom).val($(obj).parents('tr').children('td').eq(2).text());
			$("#roleDesc", dom).val($(obj).parents('tr').children('td').eq(3).text());
		},
		end:function(){
			if($('#childFormMsg').val() == "保存成功"){
				loadRoleList();
			}
		}
	});
}

/*
 * 删除
 */
function role_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'DELETE',
			url: basePath + id,
			dataType: 'json',
			success: function(result){
				$(obj).parents("tr").remove();
				layer.msg(result.message,{icon:1,time:2000});
			},
			error:function(data) {
				console.log(data.message);
			},
		});		
	});
}

/*
 * 状态更新
 */
function role_active(obj){
	var id =$(obj).parents("tr").children('td').eq(1).text(); 
	var status = $(obj).attr('title') == '启用' ? 1 : 0;
	var msg = status == 0 ? '确定要停用此角色？' : '确定要启用此角色？';
	layer.confirm(msg, function(){
		$.ajax({
			type: 'post',
			url: basePath + "active",
			dataType: 'json',
			data:{'id':id,'active':status},
			success: function(result){
				$(obj).parent("td").prev('td').html(status==0?'<span class="label label-warning radius">已停用</span>':'<span class="label label-success radius">已启用</span>');
				$(obj).attr('title',status == 0 ? '启用' : '停用');
				$(obj).find('.Hui-iconfont').html(status == 0 ? '&#xe615;' : '&#xe631;');
				layer.msg(result.message,{icon:1,time:2000});
			},
			error:function(data) {
				console.log(data.message);
			},
		});	
	});
}


/*
 * 处理角色菜单权限
 */
function role_permission(roleId){
	layer.open({
		type: 2,
		title: '编辑菜单权限',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'toPage?url=pages/admin/role/role-permission-info',
		area: ['800px', '400px'], 
		success: function (layero, index) { // 层弹出后的成功回调方法  ----  layero 当前层dom、index 当前层索引
			// 获取弹出层上对应的控件对象
			var dom = layero.find("iframe")[0].contentWindow.document;
			$('#roleId',dom).val(roleId);
			$('#moduleList',dom).html(moduleHtml);
			$.ajax({
				type: 'get',
				url: "/admin/role/permission/" + roleId,
				dataType: 'json',
				success: function(result){
					var roleVsModules = result.data['roleVsModules'];
					for(var i=0;i<roleVsModules.length;i++){
						$('input[type="checkbox"][value="'+roleVsModules[i]['operationId']+'"]',dom).attr('checked',true);
					}
				},
				error:function(data) {
					console.log(data.message);
				},
			});	
			
		}
	});
	
}

